<template>
  <div class="app-container">
    <el-row>
      <!-- 部门树 -->
      <el-col :lg="3" :xs="24" style="">
        <el-card shadow="hover">
          <el-input v-model="groupName" placeholder="请输入设备组名称" prefix-icon="Search" clearable />
          <el-tree class="mt-2" ref="deptTreeRef" node-key="id" :data="groupList"
            :props="{ label: 'groupName', children: 'children' }"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            highlight-current
            default-expand-all
            @node-click="handleNodeClick"/>
        </el-card>
      </el-col>
      <el-col :lg="20"  style="margin-left: 10px">

        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="所属物业" prop="propertyId">
            <el-select v-model="queryParams.propertyId" placeholder="请选择物业" size="mini">
              <el-option v-for="property in propertyListOptions"
                         :key="property.id"
                         :label="property.propertyName"
                         :value="property.id"/>
            </el-select>
          </el-form-item>
          <el-form-item label="所属小区" prop="estateId">
            <el-select v-model="queryParams.estateId" placeholder="请选择小区" style="width:140px;" size="mini" filterable clearable>
              <el-option v-for="estate in estatesOptions"
                         :key="estate.id"
                         :label="estate.estateName"
                         :value="estate.id"/>
            </el-select>
          </el-form-item>
          <el-form-item label="设备组" prop="groupName">
            <el-input v-model="queryParams.groupName" placeholder="请输入设备组名称" clearable size="mini"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                       v-hasPermi="['module:terminalGroup:add']">新增</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"/>
        </el-row>

        <el-table v-loading="loading" :data="groupList" row-key="id" default-expand-all
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
          <el-table-column label="设备组名称" align="center" prop="groupName" />
          <el-table-column label="物业公司" align="center" prop="propertyName" />
          <el-table-column label="所属小区" align="center" prop="estateName" />
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <div>
                <el-tag v-if="scope.row.state === 0" style="cursor: pointer" :type="'info'">停用</el-tag>
                <el-tag v-else style="cursor: pointer" :type=" '' ">正常</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                         v-hasPermi="['module:terminalGroup:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)"
                         v-hasPermi="['module:terminalGroup:add']">新增</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                         v-hasPermi="['module:terminalGroup:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

      </el-col>

    </el-row>

    <!-- 添加或修改设备组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="所属物业" prop="propertyId">
          <el-select v-model="form.propertyId" placeholder="请选择物业" size="mini">
            <el-option v-for="property in propertyListOptions"
                       :key="property.id"
                       :label="property.propertyName"
                       :value="property.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="所属小区" prop="estateId">
          <el-select v-model="form.estateId" placeholder="请选择小区" style="width:140px;" size="mini" filterable clearable>
            <el-option v-for="estate in estatesOptions"
                       :key="estate.id"
                       :label="estate.estateName"
                       :value="estate.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="设备组名称" prop="groupName">
          <el-input v-model="form.groupName" placeholder="请输入设备组名称" size="mini"/>
        </el-form-item>
        <el-form-item label="所属层级" prop="parentId">
          <treeselect v-model="form.parentId" :options="groupOptions" :normalizer="normalizer" placeholder="请选择父层级id" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state" size="mini">
            <el-radio :value="0" :label="0">停用</el-radio>
            <el-radio :value="1" :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGroup, getGroup, delGroup, addGroup, updateGroup, exportGroup } from "@/api/module/event/terminal/group";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listPropertyNoPage} from "@/api/module/property/property";
import {listAllEstate} from "@/api/module/estate/estate";

export default {
  name : "Group",
  components: {
    Treeselect
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 设备组表格数据
      groupList: [],
      // 设备组树选项
      groupOptions: [],
      // 弹出层标题
      title: "",
      groupName:'',
      // 是否显示弹出层
      open: false,
      propertyListOptions:[],
      estatesOptions:[],
      // 查询参数
      queryParams: {
        propertyId: null,
        estateId: null,
        groupName: null,
        parentId: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    }
  },
  watch: {
    // 根据名称筛选部门树
    groupName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getEstateList();
    this.getPropertyList();
  },
  methods: {

    getPropertyList() {
      listPropertyNoPage().then(response => {
        this.propertyListOptions = response.data;
      });
    },
    getEstateList() {
      listAllEstate().then(response => {
        this.estatesOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    /** 节点单击事件 */
    handleNodeClick(data){
      // this.queryParams.deptId = data.id;
      // this.handleQuery()
    },
    /** 查询设备组列表 */
    getList() {
      this.loading = true;
      listGroup(this.queryParams).then(response => {
        this.groupList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      });
    },
    /** 转换设备组数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.groupName,
        children: node.children
      };
    },
    /** 查询设备组下拉树结构 */
    getTreeselect() {
      listGroup().then(response => {
        this.groupOptions = [];
        const data = { id: 0, groupName: '主类目', children: [] }
        data.children = this.handleTree(response.data, "id", "parentId");
        this.groupOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        propertyId: null,
        estateId: null,
        groupName: null,
        parentId: null,
        state: null,
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加设备组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.id;
      }
      getGroup(row.id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改设备组";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateGroup(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addGroup(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除设备组编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delGroup(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有设备组数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportGroup(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
